<?php

namespace App\Admin\Controllers;

use App\Admin\Exporter\ApplyExport;
use App\Admin\Repositories\AdminApply;
use App\Admin\Override\Form;
use App\Admin\Override\Grid;
use App\Utils\Helper;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\Filter;

class AdminApplyController extends BaseController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::css('/css/apply.css');
        return Grid::make(new AdminApply(), function (Grid $grid) {
            $grid->column('user_name');
            $grid->column('id_card_number');
            $grid->column('phone')->style('width:200px!important;');
            $grid->column('status')->select([
                1 => '待审核',
                2 => '审核通过',
                3 => '审核不通过',
                4 => '已缴费',
                5 => '已考试',
                6 => '考证合格',
                7 => '待出证',
                8 => '已出证',
                9 => '考试不合格'
            ]);
            $grid->column('source');
            $grid->column('work_type');
            $grid->column('level');
            $grid->column('card_front_image')->image(env('APP_URL'));
            $grid->column('card_back_image')->image(env('APP_URL'));
            $grid->column('head_image')->image(env('APP_URL'));
            $grid->column('created_at');
            $grid->fixColumns(0,1);
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->export(new ApplyExport());
            $grid->quickSearch(['user_name', 'id_card_number'])->placeholder('请输入姓名、身份证');
            $grid->tools("
<div class='dropdown  float-right'>
    <a class='btn btn-primary' type='button' target='_blank' href='/dist'>批量导入</a>
  </div>");
            $grid->filter(function (Filter $filter) {
                $filter->equal('user_name');
                $filter->equal('id_card_number');
                $filter->equal('phone');
                $filter->equal('status')->select(AdminApply::STATUS);

            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        $this->js('js/showForm.js');
        $this->js('js/personal/audit/approve.js');
        return Form::make(new AdminApply(), function (Form $form) {
            $form->expend('基础信息', view('head', [
                'name' => $form->model()->user_name,
                'created_at' => $form->model()->created_at,
                'id_card_number' => $form->model()->id_card_number,
                'phone' => $form->model()->phone,
            ])->render());
            $form->expend('工作经历', view('works', [
                'work_experience' => $form->model()->work_experience,
            ])->render());
            $form->expend('详细信息', view('detail', [
                'source' => $form->model()->source,
                'work_type' => $form->model()->work_type,
                'level' => $form->model()->level,
                'work_experience' => $form->model()->work_experience,
                'card_front_image' => env('APP_URL').'/' . $form->model()->card_front_image,
                'card_back_image' => env('APP_URL').'/' . $form->model()->card_back_image,
                'head_image' => env('APP_URL').'/' . $form->model()->head_image,
                'diploma_image' => env('APP_URL').'/' . $form->model()->diploma_image,
                'social_security_image' => env('APP_URL').'/' . $form->model()->social_security_image,
                'skill_image' => !empty($form->model()->skill_image)?env('APP_URL').'/' . $form->model()->skill_image:'',
                'status_string' => AdminApply::STATUS[$form->model()->status] ?? '',
                'status' => $form->model()->status,
                'company'=>$form->model()->company,
                'age'=>$form->model()->age,
                'sex'=>$form->model()->sex,
            ])->render());
        });
    }

    public function update($id)
    {
        $status = request()->get('status');
        if (!empty($status)){
            \App\Models\AdminApply::query()->where('id', $id)->update([
                'status' => $status
            ]);
        }

        return Admin::json()
            ->redirect(Helper::getCurrentPageIndexUrl())
            ->success('成功！');
    }

    public function destroy($id)
    {
        \App\Models\AdminApply::query()->where('id',$id)->delete();
        return Admin::json()
            ->redirect(Helper::getCurrentPageIndexUrl())
            ->success('成功！');
    }
}
